package com.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Md5Util;
import com.demo.common.QueryPageParam;
import com.demo.common.Result;
import com.demo.entity.Menu;
import com.demo.entity.User;
import com.demo.service.MenuService;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author demo
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

//    @GetMapping("/list")
//    public List<User>list(){
//        return userService.list();
//    }

    @GetMapping("/list")
    public Result list(){
        List list = userService.list();
        return Result.suc(list);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return userService.save(user)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return userService.removeById(id)?Result.suc():Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }

    @GetMapping("/findByUsername")
    public Result findByUsername(@RequestParam String username){
        List list = userService.lambdaQuery().eq(User::getUsername,username).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List list = userService.lambdaQuery()
                .eq(User::getUsername,user.getUsername())
                .eq(User::getPassword, Md5Util.getMD5String(user.getPassword())).list();
//        return list.size()>0?Result.suc(list.get(0)):Result.fail();

        if(list.size()>0){
            User user1 = (User)list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuright,user1.getUserType()).list();
            HashMap res = new HashMap();
            res.put("user",user1);
            res.put("menu",menuList);
            return Result.suc(res);
        }
        return Result.fail();
        //list.size()>0?Result.suc(list.get(0)):
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public Result listP(@RequestBody User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(user.getUsername())) {
            lambdaQueryWrapper.like(User::getUsername,user.getUsername());
        }
        //lambdaQueryWrapper.like(User::getUsername,user.getUsername());
        //return  userService.list(lambdaQueryWrapper);
        return Result.suc(userService.list(lambdaQueryWrapper));
    }
    @PostMapping("/listPage")
    //public List<User> listPage(@RequestBody HashMap map){
        public List<User> listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        System.out.println("num==="+query.getPageNum());
        System.out.println("size==="+query.getPageSize());

        /*HashMap param = query.getParam();
        System.out.println("name==="+(String)param.get("name"));
        System.out.println("no==="+(String)param.get("no"));
         */
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getUsername,username);

        IPage result = userService.page(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());

        return result.getRecords();
    }

    @PostMapping("/listPageC")
    public List<User> listPageC(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        System.out.println("num==="+query.getPageNum());
        System.out.println("size==="+query.getPageSize());

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getUsername,username);

        IPage result = userService.pageC(page);

        System.out.println("total=="+result.getTotal());

        return result.getRecords();
    }

    @PostMapping("/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        String sex = (String)param.get("sex");
        String usertype = (String)param.get("usertype") ;
        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(User::getUsername,username);
        }
        if(StringUtils.isNotBlank(sex)){
            lambdaQueryWrapper.eq(User::getSex,sex);
        }
        if(StringUtils.isNotBlank(usertype)){
            lambdaQueryWrapper.eq(User::getUserType,usertype);
        }

        IPage result = userService.pageCC(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());
    }


}
